<template>
  <div class="camera_outer">
    <div class="header">
      <!-- 提示信息 -->
      <div class="faly" v-if="openVideo">
        <h3>主播暂时离开，马上回来. . .</h3>
      </div>
      <!-- 摄像头 -->
      <video id="videoCamera" autoplay></video>
      <canvas style="display: none" id="canvasCamera"></canvas>
    </div>
    <div class="main">
      <div class="button">
        <van-button @click="getCompetence()">打开摄像头</van-button>
        <van-button @click="stopNavigator()" v-if="isShow1">关闭摄像头</van-button>
        <van-button v-if="isShow2" disabled>关闭摄像头</van-button>
        <!-- 输入框 -->
      </div>
      <van-button type="text" class="outBtn" @click="OutHand">退出直播间</van-button>
    </div>
  </div>
</template>

<script>
import axios from "../../axios/index.js";
import { defineComponent, reactive } from "vue";
import { useRoute, useRouter } from "vue-router";
export default defineComponent({
  setup() {
    const router = useRouter();
    const route = useRoute();
    const stateBo = route.query;
    const OutHand = () => {
      axios
        .get("/zhiboup", {
          params: {
            img: "https://api88.net/api/img/rand/",
            name: stateBo.name,
            time: stateBo.time
          }
        })
      router.push("/bo");
    };

    return {
      OutHand
    };
  },
  data() {
    return {
      isShow1: false,
      isShow2: true,
      thisCancas: null,
      thisContext: null,
      thisVideo: null,
      openVideo: true //显示隐藏提示内容
      //发送弹幕
    };
  },
  methods: {
    // 调用权限（打开摄像头功能）
    getCompetence() {
      //隐藏提示信息
      this.openVideo = false;
      //改变isShow的值达到显示禁用启用效果
      this.isShow1 = true;
      this.isShow2 = false;

      var _this = this;
      _this.thisCancas = document.getElementById("canvasCamera");
      _this.thisContext = this.thisCancas.getContext("2d");
      _this.thisVideo = document.getElementById("videoCamera");
      _this.thisVideo.style.display = "block";
      // 获取媒体属性，旧版本浏览器可能不支持mediaDevices，我们首先设置一个空对象
      if (navigator.mediaDevices === undefined) {
        navigator.mediaDevices = {};
      }
      // 一些浏览器实现了部分mediaDevices，我们不能只分配一个对象
      // 使用getUserMedia，因为它会覆盖现有的属性。
      // 这里，如果缺少getUserMedia属性，就添加它。
      if (navigator.mediaDevices.getUserMedia === undefined) {
        navigator.mediaDevices.getUserMedia = function(constraints) {
          // 首先获取现存的getUserMedia(如果存在)
          var getUserMedia =
            navigator.webkitGetUserMedia ||
            navigator.mozGetUserMedia ||
            navigator.getUserMedia;
          // 有些浏览器不支持，会返回错误信息
          // 保持接口一致
          if (!getUserMedia) {
            //不存在则报错
            return Promise.reject(
              new Error("getUserMedia is not implemented in this browser")
            );
          }
          // 否则，使用Promise将调用包装到旧的navigator.getUserMedia
          return new Promise(function(resolve, reject) {
            getUserMedia.call(navigator, constraints, resolve, reject);
          });
        };
      }
      var constraints = {
        audio: false,
        video: {
          width: this.videoWidth,
          height: this.videoHeight,
          transform: "scaleX(-1)"
        }
      };
      navigator.mediaDevices
        .getUserMedia(constraints)
        .then(function(stream) {
          // 旧的浏览器可能没有srcObject
          if ("srcObject" in _this.thisVideo) {
            _this.thisVideo.srcObject = stream;
          } else {
            // 避免在新的浏览器中使用它，因为它正在被弃用。
            _this.thisVideo.src = window.URL.createObjectURL(stream);
          }
          _this.thisVideo.onloadedmetadata = function(e) {
            _this.thisVideo.play();
          };
        })
        .catch(err => {
          console.log(err);
        });
    },
    // 关闭摄像头
    stopNavigator() {
      this.thisVideo.srcObject.getTracks()[0].stop();
      //显示提示信息
      this.openVideo = true;
      //改变isShow的值达到显示禁用启用效果
      this.isShow1 = false;
      this.isShow2 = true;
    }
    //发送弹幕
  }
});
</script>

<style  lang="scss" scoped>
.camera_outer {
  width: 100%;
  height: 500px;
  background: #000;
  position: relative;
}
#videoCamera {
  width: 100%;
  height: 500px;
}
.faly {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: #fff;
  text-align: center;
}
//退出直播间
.outBtn {
  position: absolute;
  bottom: 3%;
  right: 5px;
  color: #000;
}
.outBtn:hover {
  color: red;
}
</style>